#北京新发地菜价抓取模拟
import requests
import csv
from bs4 import BeautifulSoup




def getContent(url):
    try:
        agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
        response=requests.get(url,headers={"User-Agent":agent})
        response.raise_for_status()
        response.encoding=response.apparent_encoding
    except Exception as e:
        print("抓取错误")
    else:
        return response.content.decode("utf-8")

def parserContent(htmlConten):
    soup=BeautifulSoup(htmlContent,"html.parser")
    print(soup.prettify())
    #list = soup.find_all("li",class_="lis")   写法不便捷,建议改成attrs={"class":"lis"}
    table = soup.find("table")  #只能用一次
    ths=table.find_all("th")
    print("--------------------------------------")
    f=open("resouce/菜价.csv",mode="w",encoding="utf-8")
    writer=csv.writer(f)
    #flag=1;
    for i in "o" :
        oneClassify=ths[0].text
        twoClassify=ths[1].text
        name=ths[2].text
        floorPrice=ths[3].text
        avgPrice=ths[4].text
        maxPrice=ths[5].text
        type=ths[6].text
        land=ths[7].text
        unit=ths[8].text
        date=ths[9].text
        writer.writerow([oneClassify,twoClassify,name,floorPrice,avgPrice,maxPrice,type,land,unit,date])
    f.close()
if __name__ == '__main__':
    url="http://www.xinfadi.com.cn/priceDetail.html"
    htmlContent=getContent(url)
    parserContent(htmlContent)